
using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using System.Web.UI;


namespace PureSystems.Modules.psSquared
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewpsSquared class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : PortalModuleBase
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {

            string strExlcudedRoles = (string)Settings["ExcludedRoles"];

            if (strExlcudedRoles != null && strExlcudedRoles != "")
            {
                // check if they are in any of the selected Roles
                if (!PortalSecurity.IsInRoles(strExlcudedRoles))
                {
                    AddTrackingCode();
                }

            }
            else
            {
                AddTrackingCode();
            }

            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        private void AddTrackingCode()
        {
            try
            {
                if (Settings["GoSquaredAcc"] != null)
                {
                    string strAccountNumber = Settings["GoSquaredAcc"].ToString();

                    string htmlOutput = @"<script type=""text/javascript"">";
                    htmlOutput += @"var GoSquared = {};";
                    htmlOutput += @"GoSquared.acct = """ + strAccountNumber + @""";";
                    htmlOutput += @"(function(w){";
                    htmlOutput += @"function gs(){";
                    htmlOutput += @"w._gstc_lt = +new Date;";
                    htmlOutput += @"var d = document, g = d.createElement(""script"");";
                    htmlOutput += @"g.type = ""text/javascript"";";
                    htmlOutput += @"g.src = ""//d1l6p2sc9645hc.cloudfront.net/tracker.js"";";
                    htmlOutput += @"var s = d.getElementsByTagName(""script"")[0];";
                    htmlOutput += @"s.parentNode.insertBefore(g, s);";
                    htmlOutput += @"}";
                    htmlOutput += @"w.addEventListener ?";
                    htmlOutput += @"w.addEventListener(""load"", gs, false) :";
                    htmlOutput += @"w.attachEvent(""onload"", gs);";
                    htmlOutput += @"})(window);";
                    htmlOutput += @"</script>";

                    LiteralControl litCookieNotice = new LiteralControl(htmlOutput);
                    this.Page.Form.Controls.Add(litCookieNotice);
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {

            if (!IsEditable)
            {
                // hide module + container when not in edit mode
                ContainerControl.Visible = false;
            }
        }

        #endregion



    }

}
